<?php

namespace common\models;

use Yii;
use common\models\Region;

/**
 * This is the model class for table "company".
 *
 * @property integer $id
 * @property string $company_name
 * @property string $company_no
 * @property integer $company_type 
 * @property string $logo
 * @property integer $province
 * @property integer $city
 * @property string $address_detail
 * @property string $contact_name
 * @property string $contact_tel
 * @property string $job_title
 * @property string $remark
 * @property string $create_time
 */
class Company extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'company';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['company_name', 'company_type'], 'required'],
            [['province','company_type', 'city'], 'integer'],
            [['create_time','status','username'], 'safe'],
            [['company_name'], 'unique'],
            [['company_name', 'company_no', 'logo', 'address_detail', 'contact_name', 'remark'], 'string', 'max' => 255],
            [['contact_tel', 'job_title'], 'string', 'max' => 50],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'company_name' => '企业名称',
            'company_no' => '企业代码',
            'company_type' => '公司类型',//：1民营、2上市、3外资、4外企、5大型', 
            'username' => '用户账号',
            'logo' => 'logo',
            'province' => '省份',
            'city' => '城市',
            'status' => '审核状态',
            'address_detail' => '详细地址',
            'contact_name' => '联系人姓名',
            'contact_tel' => '联系人电话',
            'job_title' => '联系人岗位',
            'remark' => '备注',
            'create_time' => '创建时间',
        ];
    }
    public static function region($level = 1,$id = 1)
    {

        $list = Region::find()
                ->select('name')
                ->where(['level' => $level,'id' => $id])
                ->Scalar();
        return $list;
    }

    //省的所有列表
    public static function Provincelist()
    {
        $list = Region::find()
                ->select('id,name')
                ->where(['level' => 1])
                ->asarray()
                ->all();
        foreach ($list as $k => $v) {
                    $li[$v['id']] = $v['name'];
                }
        return $li;
    }

    //传过来的省编号对应的市的列表
    public static function Citylist($id = 0)
    {
        if(!$id){
            return $li = ['37' => '北京市'];
        }else{

            $list = Region::find()
                    ->select('id,name')
                    ->where(['level'=>2,'parent_id'=>$id])
                    ->asarray()
                    ->all();
            foreach ($list as $k => $v) {
                        $li[$v['id']] = $v['name'];
                    }
            return $li;
        }
    }

    public static function itemAlias ($type, $code = NULL)
    {
        $_items = [
            'status' => [
                //''  =>'全部',
                '0' => "审核中",              
                '1' => "审核通过",
                '2' => "审核失败",
             ],
             'company_type' => [
                //''  =>'全部',
                '1' => "民营",              
                '2' => "上市",
                '3' => "外资",
                '4' => "外企",
                '5' => "大型",
             ],
          
        ];

        if (isset($code))
            return isset($_items[$type][$code]) ? $_items[$type][$code] : false;
        else
            return isset($_items[$type]) ? $_items[$type] : false;
    }

    /*
    * 保存前执行的操作
    */
    public function beforeSave($insert) {
        if (parent::beforeSave($insert)) {
            //是否是新添加
            if($this->isNewRecord) {
                
                if(!$this->username) $this->username = "";
                if(!$this->company_no) $this->company_no = 0;
                if(!$this->logo) $this->logo = "";
                if(!$this->province) $this->province = 0;
                if(!$this->city) $this->city = 0;
                if(!$this->address_detail) $this->address_detail = "";

                if(!$this->contact_name) $this->contact_name = "";
                if(!$this->contact_tel) $this->contact_tel = "";
                if(!$this->job_title) $this->job_title = "";
                if(!$this->remark) $this->remark = "";

                $this->create_time = date("Y-m-d H:i:s", time());

                //$this->update_time = date("Y-m-d H:i:s", time());
            }else{
                //$this->update_time = date("Y-m-d H:i:s", time());
            }
            return true;
        } else {
            return false;
        }
    } 
}
